package pojo.utils;

import java.util.Arrays;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

import pojo.wizards.PathWizardPage;
import pojo.wizards.TableWizardPage;

public class UiUtils {

	public static Text createText(Composite parent, String textName) {
		Label label = new Label(parent, SWT.NULL);
		label.setText(textName);
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		new Label(parent, SWT.NULL);
		return text;
	}

	public static List createOrdinalList(Composite parent) {
		List list = new List(parent, SWT.MULTI | SWT.V_SCROLL);
		list.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
		return list;
	}

	public static Button createAllToRightButton(final TableWizardPage page, Composite parent) {
		Button btn = new Button(parent, SWT.PUSH);
		btn.setText(" > > ");
		btn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				page.getSelectedList().setItems(page.getListItems().toArray(new String[page.getListItems().size()]));
				page.getOrdinalList().removeAll();
				Validator.validateEmptyItem(page);
			}
		});
		return btn;
	}

	public static Button createToRightButton(final TableWizardPage page, Composite parent) {
		Button btn = new Button(parent, SWT.PUSH);
		btn.setText("  >  ");
		btn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] selectItems = page.getOrdinalList().getSelection();
				String[] items = page.getSelectedList().getItems();
				String[] listItems = concatArray(items, selectItems);
				Arrays.sort(listItems);
				page.getSelectedList().setItems(listItems);
				for (String item : selectItems) {
					page.getOrdinalList().remove(item);
				}
				Validator.validateEmptyItem(page);
			}
		});
		return btn;
	}

	public static Button createToLeftButton(final TableWizardPage page, Composite parent) {
		Button btn = new Button(parent, SWT.PUSH);
		btn.setText("  <  ");
		btn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] selectItems = page.getSelectedList().getSelection();
				String[] items = page.getOrdinalList().getItems();
				String[] listItems = concatArray(items, selectItems);
				Arrays.sort(listItems);
				page.getOrdinalList().setItems(listItems);
				for (String item : selectItems) {
					page.getSelectedList().remove(item);
				}
				Validator.validateEmptyItem(page);
			}
		});
		return btn;
	}

	public static Button createAllToLeftButton(final TableWizardPage page, Composite parent) {
		Button btn = new Button(parent, SWT.PUSH);
		btn.setText(" < < ");
		btn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				page.getOrdinalList().setItems(page.getListItems().toArray(new String[page.getListItems().size()]));
				page.getSelectedList().removeAll();
				Validator.validateEmptyItem(page);
			}
		});
		return btn;
	}

	public static List createSelectedList(Composite parent) {
		List list = new List(parent, SWT.MULTI | SWT.V_SCROLL);
		list.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
		return list;
	}

	public static TabFolder createTabFolder(final PathWizardPage page, Composite parent) {
		TabFolder tabFolder = new TabFolder(parent, SWT.NULL);
		tabFolder.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (page.isInited()) { // NPE 请吃冰淇淋。
					 Validator.validatorEmptyString(page);
				}
			}
		});
		return tabFolder;
	}

	public static Text createJavaText(Shell shell, PathWizardPage page, Composite parent) {
		// Label
		Label label = new Label(parent, SWT.NULL);
		label.setText(I18n.makeCodeUI_javaPathLabel);
		// Text
		final Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addModifyListener(new ValidatorModifyAdapter(page));
		// Button
		Button button = new Button(parent, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new BrowseSelectionAdapter(shell, "", text));
		return text;
	}

	public static String[] concatArray(String[] aa, String[] bb) {
		String[] dd = new String[aa.length + bb.length];
		System.arraycopy(aa, 0, dd, 0, aa.length);
		System.arraycopy(bb, 0, dd, aa.length, bb.length);
		return dd;
	}
	
	static class ValidatorModifyAdapter implements ModifyListener {
		private PathWizardPage page;

		public ValidatorModifyAdapter(PathWizardPage page) {
			this.page = page;
		}

		@Override
		public void modifyText(ModifyEvent e) {
			Validator.validatorEmptyString(page);
		}
	}

	public static class BrowseSelectionAdapter extends SelectionAdapter {
		private final String title;
		private final Text text;
		private final Shell shell;

		public BrowseSelectionAdapter(Shell shell, String title, Text text) {
			this.shell = shell;
			this.title = title;
			this.text = text;
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			ContainerSelectionDialog dialog = new ContainerSelectionDialog(this.shell,
					ResourcesPlugin.getWorkspace().getRoot(), false, title);
			if (dialog.open() == ContainerSelectionDialog.OK) {
				Object[] result = dialog.getResult();
				if (result.length == 1) {
					text.setText(((Path) result[0]).toString());
				}
			}
		}
	}

	public static class Validator {

		public static void validatorEmptyString(PathWizardPage page) {
			switch (page.getTabFolder().getSelectionIndex()) {
			case 0:
				if (StringUtils.isBlank(page.getBaseModelPackageName_())) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_modelPackageNameLabel));
					return;
				}
				if (page.getBaseModelOutputDir().getText().trim().length() == 0) {
					updateStatus(page, String.format(I18n.error_empty_string, I18n.makeCodeUI_javaPathLabel));
					return;
				}
				updateStatus(page, null);
			}
		}

		public static void validateEmptyItem(TableWizardPage page) {
			if (page.getSelectedList().getItemCount() == 0) {
				updateStatus(page, I18n.tableSelectUI_pleaseSelectTable);
			} else {
				updateStatus(page, null);
			}
		}

		private static void updateStatus(WizardPage page, String message) {
			page.setErrorMessage(message);
			page.setPageComplete(message == null);
		}
	}

}
